// pages/my/service-order/service-order.js
const controllers = require('../../controllers/ControllerMy').controller;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype: '',
    orderList: [],
    activenav: 0,
    imgurl: '',
    navlist: ['全部', '待预约', '待服务', '待拼团', '待评价'],
    navall: false,
    navchoice: '',
    isshow: false,
    singleord: '',
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    pageNumber: 1,
    pageSize: 10,
    isEnd: false,
  },
  //获取列表
  orderList(state) {
    //获取服务订单列表
    controllers.getOrderList({
      pageNumber: this.data.pageNumber,
      pageSize: this.data.pageSize,
      shopId: app.globalData.shopId,
      type: 0,
      state: state || ''
    }).then(res => {
      // this.setData({
      //   orderList:res.data.list,
      //   navchoice:this.data.navchoice
      // })
      if (res.data.list.length != 0) {
        this.setData({
          orderList: this.data.orderList.concat(res.data.list)
        })
      }
      if (res.data.lastPage) {
        this.setData({
          isEnd: true
        })
      }
    })
  },
  //按钮事件
  tapDialogButton(e) {
    console.log(e);
    if (e.detail.index == 0) {
      this.setData({
        isshow: false
      })
    } else {
      this.cancelOrder()
      this.setData({
        isshow: false
      })
    }
  },
  //点击导航列表
  changenav(e) {
    this.setData({
      activenav: e.currentTarget.id
    })
    if (e.currentTarget.id == 0) {
      this.setData({
        navall: true,
        orderList: [],
        navchoice: '',
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else if (e.currentTarget.id == 1) {
      this.setData({
        navchoice: 2,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else if (e.currentTarget.id == 2) {
      this.setData({
        navchoice: 7,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else if (e.currentTarget.id == 3) {
      this.setData({
        navchoice: 1,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)
    } else {
      this.setData({
        navchoice: 8,
        navall: false,
        orderList: [],
        isEnd: false,
        pageNumber: 1,
      })
      this.orderList(this.data.navchoice)

    }



  },
  //完成服务
  toComplete(e) {
    controllers.accountOrderSuccess({
      orderNo: e.currentTarget.id,
      shopId: app.globalData.shopId,
      shopAccountId: e.currentTarget.dataset.shopaccountid || '',
      type: 0
    }).then(res => {
      wx.showToast({
        title: '成功',
        icon: 'none'
      })
      this.setData({
        pageNumber:1,
        pageSize:10,
        isEnd:false,
        orderList:[]
      })
      this.orderList(this.data.navchoice)
      //获取服务订单列表
      // controllers.getOrderList({
      //   pageNumber: 1,
      //   pageSize: 20,
      //   shopId: app.globalData.shopId,
      //   type: 0
      // }).then(res => {
      //   this.setData({
      //     orderList: res.data.list,
      //     navchoice: this.data.navchoice
      //   })
      // })
    })
  },
  //显示取消确定弹窗
  toshow(e) {
    this.setData({
      isshow: true,
      singleord: e.currentTarget.id
    })
  },
  //取消订单
  cancelOrder(e) {
    controllers.cancelOrder({
      shopId: app.globalData.shopId,
      orderNo: this.data.singleord,
      type: 0
    }).then(res => {
      wx.showToast({
        title: '取消成功',
        icon: 'none'
      })
      this.setData({
        pageNumber:1,
        pageSize:10,
        isEnd:false,
        orderList:[]
      })
      this.orderList(this.data.navchoice)
      //获取服务订单列表
      // controllers.getOrderList({
      //   pageNumber: 1,
      //   pageSize: 20,
      //   shopId: app.globalData.shopId,
      //   type: 0
      // }).then(res => {
      //   this.setData({
      //     orderList: res.data.list,
      //     navchoice: this.data.navchoice
      //   })
      // })
    })
  },
  //跳转到订单详情页
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/my/serviceOrder-detail/serviceOrder-detail?id=' + e.currentTarget.id,
    })
  },
  //跳转到预约选择页面
  toShopDetail(e) {
    let orderNo=e.currentTarget.dataset.orderno

    wx.navigateTo({
      url: '/pages/Index/Service?goodsid=' + e.currentTarget.dataset.goodsid + '&orderId=' + e.currentTarget.dataset.orderid+"&orderNo="+orderNo,
    })

    // wx.requestSubscribeMessage({
    //   tmplIds:['Gc_gkdb1wX-cSBFPD4D5D31w6ZOojJeDckkzo49pBmQ','FKMu-6I96hfuWhGpwb-JxdAKmtI1j013G4GTy2rz7Ms','hD6D4MmAd2JDgrivAhrWDoOXweER5uf9ONSwryEXVe0'],
    //   success:res=>{
    //     console.log(res)
    //   },
    //   complete:()=>{
    //     wx.navigateTo({
    //       url: '/pages/Index/Service?goodsid=' + e.currentTarget.dataset.goodsid + '&orderId=' + e.currentTarget.dataset.orderid+"&orderNo="+orderNo,
    //     })
    //   }
    // })
  },

  //跳转到评论页面
  toComment(e) {
    wx.navigateTo({
      url: '/pages/my/service-comment/service-comment?id=' + e.currentTarget.id + '&type=' + e.currentTarget.dataset.type + '&from=' + e.currentTarget.dataset.from,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      globalStype: app.globalData.globalStype
    })
    this.setData({
      imgurl: app.globalData.IMG_URL
    })
    this.setData({
      navchoice: options.id
    })
    if(options.id==9){
      this.setData({
        navchoice: ''
      })
    }

    if (options.id == 2) {
      this.setData({
        activenav: 1
      })
    } else if (options.id == 7) {
      this.setData({
        activenav: 2
      })
    } else if (options.id == 9) {
      this.setData({
        navall: true
      })
    } else if (options.id == 8) {
      this.setData({
        activenav: 4
      })
    } else if (options.id == 1) {
      this.setData({
        activenav: 3
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      orderList: [],
      isEnd: false,
      pageNumber: 1,
    })
    this.orderList(this.data.navchoice)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      });
      this.orderList(this.data.navchoice)
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})